#include "Components/ComponentInventory.h"
#include "Managers/BlocksManager.h"
#include "Terrain/Terrain.h"

namespace PocketSurvival
{
	void ComponentInventory::setActiveSlotIndex(uint32_t slotIndex)
    {
        activeSlotIndex = MathUtils::Clamp<uint32_t>(slotIndex, 0, getVisibleSlotsCount() - 1);
    }
	uint32_t ComponentInventory::getActiveSlotIndex()
    {
        return activeSlotIndex;
    }
	void ComponentInventory::setVisibleSlotsCount(uint32_t count)
    {
    }
	uint32_t ComponentInventory::getVisibleSlotsCount()
    {
        return 7;
    }
    uint32_t ComponentInventory::getSlotCapacity(uint32_t slotIndex, int32_t cellValue)
    {
        if(slotIndex >= getVisibleSlotsCount() && slotIndex < 10)
		{
			return 0;
		}
		return GameSingleton::blocksManager->blocks[Terrain::ExtractContents(cellValue)]->getMaxStacking(cellValue);
    }

    static const std::string componentName = "Inventory";
    const std::string &ComponentInventory::getName() const
    {
        return componentName;
    }
    const std::string &ComponentInventory::GetName()
    {
        return componentName;
    }
    void ComponentInventory::load(const nlohmann::json &jsonData)
	{
		ComponentInventoryBase::load(jsonData);
        activeSlotIndex = jsonData["ActiveSlotIndex"].get<uint32_t>();
	}
    void ComponentInventory::save(nlohmann::json &jsonData)
	{
        ComponentInventoryBase::save(jsonData);
        jsonData["ActiveSlotIndex"] = activeSlotIndex;
	}
	void ComponentInventory::saveToMsgJson(nlohmann::json &jsonData)
	{
        ComponentInventoryBase::saveToMsgJson(jsonData);
        MSG_VALUE_SET(jsonData, "ActiveSlotIndex", "int", activeSlotIndex);
	}

} // namespace PocketSurvival
